package com.atguigu;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class App {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //1.创建MinioClient对象。我们的操作都是通过这个核心对象进行的
        MinioClient minioClient = MinioClient.builder()
                .credentials("minioadmin","minioadmin")
                .endpoint("http://192.168.71.128:9000").build();

        //2.判断指定名称的桶是否存在
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket("b002").build());

        //3.如果不存在，则先创建桶并设置策略
        if(!flag){
            //创建桶
            minioClient.makeBucket(MakeBucketArgs.builder()
                    .bucket("b002")
                    .build());
            //指定策略
            String policy = """
                    {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Effect": "Allow",
                                "Principal": {
                                    "AWS": [
                                        "*"
                                    ]
                                },
                                "Action": [
                                    "s3:GetObject"
                                ],
                                "Resource": [
                                    "arn:aws:s3:::b002/*"
                                ]
                            }
                        ]
                    }
                    """;
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket("b002")
                    .config(policy).build());
        }

        //4.上传图片
        minioClient.uploadObject(UploadObjectArgs.builder()
                        .bucket("b002")
                        .object("公寓-健身房.jpg")
                        .filename("D:\\atguigu2021\\2023课件\\05.后端工程化\\11.新项目一尚庭公寓\\资料\\图片资源\\7.images\\公寓-健身房.jpg").build());

    }
}
